/** open iframeChart **/
var chart_iframe = $('#iframeChart');
Core.Sys.openIframe =  function(URL, data) {
	if(!chart_iframe.hasClass('show')) {
		analysisResized();
		chart_iframe.addClass('show animated').css({
			"right": 0
		});
		chart_iframe.find('iframe').attr('src', URL);
		chart_iframe.find('.head-title b').html(data);
	}
}
function analysisResized() {
	var _width = parseFloat($('.table_box').css('width')) - parseFloat($('.datagrid-header-row td[field="title"]').css('width')) - 30;
	if(!chart_iframe.hasClass('show')) {
		chart_iframe.removeClass('animated').css({
			'width': _width,
			'right': _width * -1 -20
		});
	} else {
		chart_iframe.css({
			'width': _width
		});
	}
    /** close iframeChart **/
	chart_iframe.find('.head-title span').unbind('click').click(function() {
		chart_iframe.removeClass('show').css({
			"right": -_width -20
		});
	})
}

/** resize iframeChart **/
$(window).on('resize',function(){
    analysisResized();
})

/** close iframeChart **/
chart_iframe.find('.head-title span').click(function() {
	$('#iframed').removeClass('animated').animate({
		"right": -1020
	});
});


/**
 * 表格中配置[first row td] [first column td] click event
 */
var jsoned={};/* 图表数据初始设置 */
Core.Sys.rowChart = {};
Core.Sys.getChartJson = function(row){
    /**获取行数据 */
    jsoned.month = {fuller:{data:[]},conversion:{data:[]},Electric:{data:[]}};
    var getDataArr = function(v){
        v = v.toFixed(2);
        return [{"data":[v,((100-v)>0?(100-v):0).toFixed(2)]}];
    }
    var _seriesData=[];
    var _xAxis=[];
    var rcData = Core.Sys.rowChart;
    var arr_a = rcData.a;
    var arr_b = rcData.b;
    if(typeof row[arr_b.num] == 'undefined'){
        var attTemp = arr_b.num.toString().split('-');
        var b= row[attTemp[0]];
        var d= row[attTemp[1]];
		/*意向客转化率 成交量除以有效线索量*/
		row[arr_b.num] = (b==0||d==0)? 0: (b/d*100);
    }

    var arr_c = rcData.c;
	for(var i in arr_a){
		_seriesData.push(row[arr_a[i].num] || 0);
		_xAxis.push(arr_a[i].name);
    }
    jsoned.month.fuller.data = [{
					"series": [{"data":_seriesData}],
					"legend": [],
					"xAxis": _xAxis,
					"name": ""
				}];
    jsoned.month.conversion.data = [{
					"series": getDataArr(row[arr_b.num] || 0),
					"legend": [],
					"xAxis": arr_b.xAxis,
					"name": arr_b.name
				}];
    jsoned.month.Electric.data = arr_c.map(function(itm, idx){
        return {
            "series": getDataArr(row[itm.num] || 0),
            "legend": [],
            "xAxis": itm.xAxis,
		    "name": itm.name
        }
    })
}
Core.Sys.setColCell = function(_url, row,value,fun,imgNum){
    //最左列 数据被点击
	if(value=='合计' || value==''){
		var str=value;
	}else{
		var str='<a class="named" onclick=\'Core.Sys.'+fun+'(\"'+_url+'\",'+JSON.stringify(row)+',\"'+value+'\")\'>'+value+'<i class="vimg vico'+imgNum+'"></i></a>'
	}
	return str;
}
Core.Sys.setTdCell = function(_url,value,index,field,title){
    //第一行 单元格被点击
	value = !value?0:value;
	var str='<span class="'+field+'">'+value+'</span>';
	if(index==0){
		str='<a onclick="Core.Sys.Chart(\''+_url+'\',\''+field+'\',\''+title+'\')">'+value+'<i class="vimg vico1"></i></a>'
	}
	return str;
}
Core.Sys.Funneled = function(_url, row, title) {
    //最左列 数据被点击 打开图表 行
	Core.Sys.getChartJson(row);
	Core.Sys.openIframe(_url+"?CTYPE=rowchart", title);
}
Core.Sys.Chart = function(_url, fkey, title) {
    //第一行单元格被点击 打开图表 列
	var name=[];
	var keyed=[];
	$('.named').each(function(i,p){
		name.push($(p).text());
	})
	$('.'+fkey).each(function(i,p){
		keyed.push(parseFloat($(p).text()));
	})
	jsoned={
		"xAxis":name,
		"series":[{
			"data":keyed
		}]
	}
	if(!Core.rule('key', fkey)) return false;
	Core.Sys.openIframe(_url+"?CTYPE=colchart", title);
}
Core.Sys.dvSales = function(_url, row,title) {
    //打开图表 行
	var salesid = row['id']; //经销商id
	// var _stime = $('.input-text[name="stime"]').val();
	// var _etime = $('.input-text[name="etime"]').val();
    // var _now_infocgkey = $('.infocgkey_select').getZsValue(); /**业务渠道 */
    Core.Sys.openIframe(_url+"?thisRow="+JSON.stringify(row)+"&sales="+salesid, title);
    //搜索参数
    //ant("{$_G['_mc_'] ?? ''}/chart/nkey/{$nkey}/ckey/{$ckey}?stime="+_stime+"&etime="+_etime+"&frowkey="+JSON.stringify(fkey)+"&sales="+salesid+"&infocgkey="+_now_infocgkey, title);
}